import phaser from 'phaser';
import configer from './configer';

/** 虚拟摇杆 */
export default class Joystick {
    private scene: Phaser.Scene = null!;
    private directionHandleContainer: Phaser.GameObjects.Container = null!;

    private callbackUp: Function = null!;
    private callbackDown: Function = null!;
    private callbackLeft: Function = null!;
    private callbackRight: Function = null!;
    private callbackMid: Function = null!;

    constructor(scene: Phaser.Scene, positionX: number, positionY: number, width: number, height: number) {
        this.scene = scene;
        let handleWidth: number = width / 3
        let handleHeight: number = height / 3

        let directionHandleBackgound = this.scene.add.graphics().fillStyle(0xFFFFFF, configer.DIRECTION_HANDLE_ALPHA).fillEllipse(0, 0, width, height);
        let directionHandleOption = this.scene.add.container(0, 0,
            [
                this.scene.add.graphics().fillStyle(0xFFFFFF, 0.8).fillEllipse(0, 0, handleWidth, handleHeight),
                this.scene.add.graphics().fillStyle(0xFFA042, 0.8).fillEllipse(0, 0, handleWidth - 4, handleHeight - 4)
            ]
        );
        this.directionHandleContainer = this.scene.add.container(
            positionX,
            positionY,
            [
                directionHandleBackgound,
                directionHandleOption
            ]
        );


        directionHandleOption.setSize(handleWidth, handleHeight);
        directionHandleOption.setInteractive({ draggable: true });
        directionHandleOption.on(phaser.Input.Events.DRAG, (pointer, dragX: number, dragY: number) => {
            directionHandleOption.setPosition(dragX, dragY);

            if (directionHandleOption.y < 0) {
                if (this.callbackUp) {
                    this.callbackUp();
                }
            }
            if (directionHandleOption.y > 0) {
                if (this.callbackDown) {
                    this.callbackDown();
                }
            }
            if (directionHandleOption.x < 0) {
                if (this.callbackLeft) {
                    this.callbackLeft();
                }
            }
            if (directionHandleOption.x > 0) {
                if (this.callbackRight) {
                    this.callbackRight();
                }
            }

            if (directionHandleOption.x == 0 && directionHandleOption.y == 0) {
            }


        });
        directionHandleOption.on(phaser.Input.Events.DRAG_END, (pointer) => {
            directionHandleOption.setPosition(0, 0);
            if (this.callbackMid) {
                this.callbackMid();
            }
        });
    }

    public onUp(callback: Function) {
        this.callbackUp = callback;
    }
    public onDown(callback: Function) {
        this.callbackDown = callback;
    }
    public onLeft(callback: Function) {
        this.callbackLeft = callback;
    }
    public onRight(callback: Function) {
        this.callbackRight = callback;
    }
    public onMiddle(callback: Function) {
        this.callbackMid = callback;
    }

    destroy() {
        this.directionHandleContainer.destroy();
    }

}